package com.lecture.java8.gui;

import javax.swing.*;
import javax.swing.border.EmptyBorder;
import javax.swing.table.DefaultTableModel;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
public class UserListView extends JFrame {

    private JPanel contentPane;
    private static JTable table;
    private JTextField nameText;

    private static Service service = new Service();

    /**
     * Launch the application.
     */
    /**
     * Create the frame.
     */
    public UserListView() {

        setTitle("timetable");
        setResizable(true);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setBounds(100, 100, 650, 337);
        setLocationRelativeTo(null);

        contentPane = new JPanel();
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
        setContentPane(contentPane);
        contentPane.setLayout(null);

        JScrollPane scrollPane = new JScrollPane();
        scrollPane.setBounds(10, 39, 564, 232);
        contentPane.add(scrollPane);

        Object[] columns = {"date", "id", "name", "starting", "lasting"};// 字段
        Object[][] data = null;// 需要展示的数据，一般是二维数组
        DefaultTableModel model = new DefaultTableModel(data, columns);
        table = new JTable(model);
        //加载学生数据
        load(0);
        scrollPane.setViewportView(table);

        JLabel lblNewLabel = new JLabel("id");
        lblNewLabel.setBounds(10, 10, 42, 15);
        contentPane.add(lblNewLabel);

        nameText = new JTextField();
        nameText.setBounds(44, 8, 115, 21);
        contentPane.add(nameText);
        nameText.setColumns(10);

        //查询按钮
        JButton searchBtn = new JButton("Search");
        searchBtn.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                if (nameText.getText() != "") {
                    load(Integer.valueOf(nameText.getText()));
                }
            }
        });
        searchBtn.setBounds(169, 8, 80, 23);
        contentPane.add(searchBtn);

        //添加按钮
        JButton addBtn = new JButton("Add");
        addBtn.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                AddView view = new AddView();
                view.setVisible(true);
            }
        });
        addBtn.setBounds(300, 8, 70, 23);
        contentPane.add(addBtn);

        //修改按钮
        JButton updateBtn = new JButton("Update");
        updateBtn.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                // 获取选中行
                int row = table.getSelectedRow();
                if (row < 0) {
                    JOptionPane.showMessageDialog(contentPane, "please select a record", "System", JOptionPane.WARNING_MESSAGE);
                    return;
                }
                int id = Integer.parseInt(table.getValueAt(row, 1).toString());
                //int id = Integer.valueOf(table.getValueAt(row, 0).toString().substring(0, 4) + table.getValueAt(row, 0).toString().substring(5));
                update view = new update(id);
                view.setVisible(true);

            }
        });
        updateBtn.setBounds(390, 8, 80, 23);

        //删除按钮
        JButton deleteBtn = new JButton("delete");
        deleteBtn.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                // 获取选中行
                int row = table.getSelectedRow();
                if (row < 0) {
                    JOptionPane.showMessageDialog(contentPane, "please select a record", "System", JOptionPane.WARNING_MESSAGE);
                    return;
                }
                Object[] options = {"Confirm", "Cancel"};
                int result = JOptionPane.showOptionDialog(contentPane, "Confirm deletion？", "System",
                        JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE, null, options, options);
                if (result == 0) {
                    int id = Integer.valueOf(table.getValueAt(row, 1).toString());
                    boolean flag = service.deletion(id);
                    if (flag) {
                        JOptionPane.showMessageDialog(contentPane, "Deletion Successful!");
                        load(0);
                    } else {
                        JOptionPane.showMessageDialog(contentPane, "Fail to delete!", "System", JOptionPane.WARNING_MESSAGE);

                    }
                }
                return;
            }
        });
        deleteBtn.setBounds(485, 8, 80, 23);
        contentPane.add(deleteBtn);
        contentPane.add(updateBtn);
    }

    // 填充表格数据
    public static void load(int id) {
        ArrayList<User> list;
        DefaultTableModel tableModel;
        if (id == 0) {
            list = service.getTime("select * from `timetable`;");
        } else {
            list = service.getTime("select * from `timetable` where `id`=" + id + ";");
            }
            tableModel = (DefaultTableModel) table.getModel();
            tableModel.setRowCount(0);// 清除原有行
            if (list != null) {// 填充数据
                for (User item : list) {
                    String[] arr = new String[5];
                    arr[0] = item.getDate() / 10000 + "-" + item.getDate() % 10000;
                    arr[1] = String.valueOf(item.getId());
                    arr[2] = item.getName();
                    arr[3] = item.getStarting();
                    arr[4] = String.valueOf(item.getLasting());
                    tableModel.addRow(arr);
                }
            }
        }
    }

